package cn.iocoder.yudao.module.iot.dal.mysql.operationhistory;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.iot.controller.admin.operationhistory.vo.OperationHistoryPageReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.operationhistory.OperationHistoryDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 操作历史记录 Mapper
 *
 * @author 云智节
 */
@Mapper
public interface OperationHistoryMapper extends BaseMapperX<OperationHistoryDO> {

    // Admin端分页查询
    default PageResult<OperationHistoryDO> selectPage(OperationHistoryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OperationHistoryDO>()
                .eqIfPresent(OperationHistoryDO::getOperationCode, reqVO.getOperationCode())
                .likeIfPresent(OperationHistoryDO::getOperationName, reqVO.getOperationName())
                .eqIfPresent(OperationHistoryDO::getOperationDescription, reqVO.getOperationDescription())
                .eqIfPresent(OperationHistoryDO::getHttpMethod, reqVO.getHttpMethod())
                .eqIfPresent(OperationHistoryDO::getEndpoint, reqVO.getEndpoint())
                .eqIfPresent(OperationHistoryDO::getParameters, reqVO.getParameters())
                .eqIfPresent(OperationHistoryDO::getRequestBody, reqVO.getRequestBody())
                .eqIfPresent(OperationHistoryDO::getResponseStatus, reqVO.getResponseStatus())
                .eqIfPresent(OperationHistoryDO::getResponseBody, reqVO.getResponseBody())
                .eqIfPresent(OperationHistoryDO::getUserId, reqVO.getUserId())
                .likeIfPresent(OperationHistoryDO::getUserName, reqVO.getUserName())
                .eqIfPresent(OperationHistoryDO::getIpAddress, reqVO.getIpAddress())
                .eqIfPresent(OperationHistoryDO::getUserAgent, reqVO.getUserAgent())
                .eqIfPresent(OperationHistoryDO::getExecutionTimeMs, reqVO.getExecutionTimeMs())
                .eqIfPresent(OperationHistoryDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OperationHistoryDO::getErrorMessage, reqVO.getErrorMessage())
                .betweenIfPresent(OperationHistoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OperationHistoryDO::getId));
    }

    // App端分页查询（按用户ID过滤）
    default PageResult<OperationHistoryDO> selectPageByUserId(OperationHistoryPageReqVO reqVO, Long userId) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OperationHistoryDO>()
                .eqIfPresent(OperationHistoryDO::getOperationCode, reqVO.getOperationCode())
                .likeIfPresent(OperationHistoryDO::getOperationName, reqVO.getOperationName())
                .eqIfPresent(OperationHistoryDO::getOperationDescription, reqVO.getOperationDescription())
                .eqIfPresent(OperationHistoryDO::getHttpMethod, reqVO.getHttpMethod())
                .eqIfPresent(OperationHistoryDO::getEndpoint, reqVO.getEndpoint())
                .eqIfPresent(OperationHistoryDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OperationHistoryDO::getCreator, userId)
                .betweenIfPresent(OperationHistoryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OperationHistoryDO::getId));
    }

}